package dk.simonvogensen.itodo.shared.model;

import com.google.gwt.user.client.rpc.IsSerializable;
import dk.simonvogensen.itodo.client.model.ProjectId;
import dk.simonvogensen.itodo.client.model.TransportType;

import javax.jdo.annotations.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author $LastChangedBy:$ $LastChangedDate:$
 * @version $Revision:$
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Config implements Serializable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String id;

    @Persistent
    private String email;

    @Persistent
    private String projectIds = "project1;project2";

    @Persistent
    private String transportTypes = "office=30;car=60";

    @Persistent
    private String employeeId;

    public Config() {
    }

    public Config(String transportTypes, String projectIds) {
        this.transportTypes = transportTypes;
        this.projectIds = projectIds;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getProjectIds() {
        return projectIds;
    }

    public void setProjectIds(String projectIds) {
        this.projectIds = projectIds;
    }

    public String getTransportTypes() {
        return transportTypes;
    }

    public void setTransportTypes(String transportTypes) {
        this.transportTypes = transportTypes;
    }

    public List<ProjectId> getProjectIdList() {
        String[] projectIdArray = projectIds.split(";");
        List<ProjectId> projectIdList = new ArrayList<ProjectId>();
        for (String projectId : projectIdArray) {
            projectIdList.add(new ProjectId(projectId));
        }
        return projectIdList;
    }

    public List<TransportType> getTransportTypeList() {
        String[] transportTypeArray = transportTypes.split(";");
        List<TransportType> transportTypeList = new ArrayList<TransportType>();
        for (String transportType : transportTypeArray) {
            String[] transportTypeData = transportType.split("=");
            transportTypeList.add(new TransportType(transportTypeData[0], Integer.parseInt(transportTypeData[1])));
        }
        return transportTypeList;
    }

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }

    public String getDefaultTransportType() {
        return getTransportTypeList().get(0).getName();
    }
}
